<?php

namespace app\task;

use zhanshop\App;
use zhanshop\Helper;
use zhanshop\Log;

class VisitReportTask
{
    /**
     * 统计数据
     * @param string $app
     * @param string $channel
     * @param int $startTime
     * @param int $endTime
     * @return array
     */
    public static function statsAppData(string $app, string $channel, int $startTime, int $endTime)
    {
        if($channel == 'all'){
            $累计安装 = App::database()->model("visit_device")->where(['app' => $app])->count();
        }else{
            $累计安装 = App::database()->model("visit_device")->where(['app' => $app, 'channel' => $channel])->count();
        }


        if($channel == 'all'){
            $当日安装 = App::database()->model("visit_device")->where(['app' => $app])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }else{
            $当日安装 = App::database()->model("visit_device")->where(['app' => $app, 'channel' => $channel])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }




        if($channel == 'all'){
            $累计注册 = App::database()->model("visit_register")->where(['app' => $app])->count();
        }else{
            $累计注册 = App::database()->model("visit_register")->where(['app' => $app, 'channel' => $channel])->count();
        }




        if($channel == 'all'){
            $当日新增注册 = App::database()->model("visit_register")->where(['app' => $app])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }else{
            $当日新增注册 = App::database()->model("visit_register")->where(['app' => $app, 'channel' => $channel])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }




        if($channel == 'all'){
            $日活设备 = App::database()->model("visit_start")->where(['app' => $app])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }else{
            $日活设备 = App::database()->model("visit_start")->where(['app' => $app, 'channel' => $channel])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }




        if($channel == 'all'){
            $累计支付数 = App::database()->model("visit_pay")->where(['app' => $app])->count();
        }else{
            $累计支付数 = App::database()->model("visit_pay")->where(['app' => $app, 'channel' => $channel])->count();
        }




        if($channel == 'all'){
            $当日支付数 = App::database()->model("visit_pay")->where(['app' => $app])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }else{
            $当日支付数 = App::database()->model("visit_pay")->where(['app' => $app, 'channel' => $channel])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }



        if($channel == 'all'){
            $累计支付金额 = App::database()->model("visit_pay")->where(['app' => $app])->sum('amount');
        }else{
            $累计支付金额 = App::database()->model("visit_pay")->where(['app' => $app, 'channel' => $channel])->sum('amount');
        }



        if($channel == 'all'){
            $当日支付金额 = App::database()->model("visit_pay")->where(['app' => $app])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->sum('amount');
        }else{
            $当日支付金额 = App::database()->model("visit_pay")->where(['app' => $app, 'channel' => $channel])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->sum('amount');
        }



        if($channel == 'all'){
            $累计支付调起 = App::database()->model("visit_order")->where(['app' => $app])->count();
        }else{
            $累计支付调起 = App::database()->model("visit_order")->where(['app' => $app, 'channel' => $channel])->count();
        }



        if($channel == 'all'){
            $当日支付调起 = App::database()->model("visit_order")->where(['app' => $app])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }else{
            $当日支付调起 = App::database()->model("visit_order")->where(['app' => $app, 'channel' => $channel])->whereRaw('create_time >= '.$startTime.' and create_time <= '.$endTime)->count();
        }

        $data = [
            'app' => $app,
            'channel' => $channel,
            'dateof' => date('Y-m-d', $startTime),
            'total_install' => $累计安装,
            'install' => $当日安装,
            'total_register' => $累计注册,
            'register' => $当日新增注册,
            'active_device' => $日活设备,
            'total_payment' => $累计支付数,
            'payment' => $当日支付数,
            'total_amount' => $累计支付金额,
            'amount' => $当日支付金额,
            'total_paycall' => $累计支付调起,
            'paycall' => $当日支付调起,
            'create_time' => $startTime
        ];
        return $data;
    }
    /**
     * 每日统计
     * @return void
     */
    public function statday()
    {
        $list = App::database()->model("ad_channel")->field('app,name')->order("create_time asc")->limit(2000)->select();
        list($startTime, $endTime) = Helper::dayTimes('-1 day');
        $channel = [];
        foreach ($list as $v) {
            $channel[$v['app']] = 1;
            $insertData = $this->statsAppData($v['app'], $v['name'], $startTime, $endTime);
            try {
                App::database()->model("visit_day_report")->insert($insertData);
            }catch (\Throwable $err){
                Log::errorLog(5, substr($err->getMessage(), 0, 200));
            }
        }

        foreach ($channel as $app => $v) {
            $insertData = $this->statsAppData($app, "all", $startTime, $endTime);
            try {
                App::database()->model("visit_day_report")->insert($insertData);
            }catch (\Throwable $err){
                Log::errorLog(5, substr($err->getMessage(), 0, 200));
            }
        }
    }

    /**
     * 今日统计
     * @return void
     */
    public function statnow()
    {
        $list = App::database()->model("ad_channel")->field('app,name')->order("create_time asc")->limit(2000)->select();
        list($startTime, $endTime) = Helper::dayTimes();
        $channel = [];
        foreach ($list as $v) {
            $channel[$v['app']] = 1;
            $insertData = $this->statsAppData($v['app'], $v['name'], $startTime, $endTime);
            $insertData['update_time'] = time();
            $id = App::database()->model("visit_now_report")->where(['app' => $v['app'], 'channel' => $v['name']])->value("id");
            if($id){
                App::database()->model("visit_now_report")->where(['id' => $id])->update($insertData);
            }else{
                App::database()->model("visit_now_report")->insert($insertData);
            }
        }

        foreach ($channel as $app => $v) {
            $insertData = $this->statsAppData($app, 'all', $startTime, $endTime);
            $insertData['update_time'] = time();
            $id = App::database()->model("visit_now_report")->where(['app' => $app, 'channel' => 'all'])->value("id");
            if($id){
                App::database()->model("visit_now_report")->where(['id' => $id])->update($insertData);
            }else{
                App::database()->model("visit_now_report")->insert($insertData);
            }
        }
    }
}